<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .container {
            width: 100%;
            max-width: 400px;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #333;
        }
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #2980b9;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .links {
            margin-top: 20px;
            text-align: center;
        }
        .links a {
            color: #3498db;
            text-decoration: none;
            margin: 0 10px;
        }
        .links a:hover {
            text-decoration: underline;
        }
        .tab-container {
            display: flex;
            margin-bottom: 20px;
            border-bottom: 1px solid #ddd;
        }
        .tab {
            padding: 10px 20px;
            cursor: pointer;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            border-bottom: none;
            border-radius: 4px 4px 0 0;
            margin-right: 5px;
        }
        .tab.active {
            background-color: #fff;
            border-bottom: 1px solid #fff;
            margin-bottom: -1px;
            font-weight: bold;
        }
        .tab-content {
            display: none;
        }
        .tab-content.active {
            display: block;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>用户登录系统</h1>
        
        <div class="tab-container">
            <div class="tab active" data-tab="login">登录</div>
            <div class="tab" data-tab="register">注册</div>
        </div>
        
        <div id="login" class="tab-content active">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" placeholder="请输入用户名">
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" placeholder="请输入密码">
            </div>
            <button id="loginBtn">登录</button>
            <div id="loginResult" class="result"></div>
        </div>
        
        <div id="register" class="tab-content">
            <div class="form-group">
                <label for="regUsername">用户名:</label>
                <input type="text" id="regUsername" placeholder="请输入用户名">
            </div>
            <div class="form-group">
                <label for="regPassword">密码:</label>
                <input type="password" id="regPassword" placeholder="请输入密码">
            </div>
            <button id="registerBtn">注册</button>
            <div id="registerResult" class="result"></div>
        </div>
        
        <div class="links">
            <a href="/api-docs.html" target="_blank">API文档</a>
        </div>
    </div>

    <script>
        // 切换标签页
        document.querySelectorAll('.tab').forEach(tab => {
            tab.addEventListener('click', function() {
                // 移除所有标签页和内容的active类
                document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
                document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
                
                // 添加active类到当前标签页和对应内容
                this.classList.add('active');
                document.getElementById(this.getAttribute('data-tab')).classList.add('active');
            });
        });

        // 登录功能
        document.getElementById('loginBtn').addEventListener('click', function() {
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();
            const resultElement = document.getElementById('loginResult');
            
            // 简单验证
            if (!username || !password) {
                resultElement.textContent = '用户名和密码不能为空';
                resultElement.className = 'result error';
                resultElement.style.display = 'block';
                return;
            }
            
            // 发送登录请求
            fetch('/api/users/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    resultElement.textContent = `${data.message}，欢迎 ${data.username}！`;
                    resultElement.className = 'result success';
                } else {
                    resultElement.textContent = data.message;
                    resultElement.className = 'result error';
                }
                resultElement.style.display = 'block';
            })
            .catch(error => {
                resultElement.textContent = '登录请求失败: ' + error.message;
                resultElement.className = 'result error';
                resultElement.style.display = 'block';
            });
        });

        // 注册功能
        document.getElementById('registerBtn').addEventListener('click', function() {
            const username = document.getElementById('regUsername').value.trim();
            const password = document.getElementById('regPassword').value.trim();
            const resultElement = document.getElementById('registerResult');
            
            // 简单验证
            if (!username || !password) {
                resultElement.textContent = '用户名和密码不能为空';
                resultElement.className = 'result error';
                resultElement.style.display = 'block';
                return;
            }
            
            // 发送注册请求
            fetch('/api/users/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            })
            .then(response => response.text())
            .then(data => {
                if (data.includes('成功')) {
                    resultElement.textContent = data;
                    resultElement.className = 'result success';
                } else {
                    resultElement.textContent = data;
                    resultElement.className = 'result error';
                }
                resultElement.style.display = 'block';
            })
            .catch(error => {
                resultElement.textContent = '注册请求失败: ' + error.message;
                resultElement.className = 'result error';
                resultElement.style.display = 'block';
            });
        });
    </script>
</body>
</html>